# _*_ coding : utf-8 _*_
# @Time : 2023/8/24 20:12
# @Author : 寻梦
# @File : 07_post请求百度翻译
# @Project : code
import json
import time
# https://fanyi.baidu.com/langdetect 内容语言自动检测接口
# https://fanyi.baidu.com/v2transapi 翻译接口
import urllib.request
import urllib.parse

query = input("请输入你要翻译的内容：\n")

langdetect_url="https://fanyi.baidu.com/langdetect"
# 创建请求对象
langdetectRequestObject=urllib.request.Request(url=langdetect_url,data=urllib.parse.urlencode({"query":query}).encode("utf8"))
langdetectResponse= urllib.request.urlopen(langdetectRequestObject)
langdetectres= json.loads(langdetectResponse.read().decode('utf8'))
lan = 'zh'
if (langdetectres["error"]==0):
    # print(langdetectres["lan"])
    lan=langdetectres["lan"]
base_url='https://fanyi.baidu.com/v2transapi'
param={
    "from":lan,
    "to":"en"
}
data={
    "from": "zh",
    "to":"en",
    "query": query,
    "simple_means_flag": 3,
    "sign": "631232.631232",
    "token": "c303fc98d1fc53a83e640a8f39d72288",
    "domain": "common",
    "ts": int(time.time()*1000)
}
headers={
    "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36 SLBrowser/8.0.1.5162 SLBChan/30",
    "Cookie": "BIDUPSID=06FD1A78EAD06E363980CEA0035165CE; PSTM=1644219442; BAIDUID=EB6DC9353F2DD763B13070C217387213:FG=1; BDUSS=BsMWdXOEc4czVESXI1fkFsaEZXZVBYdUlFQjBwUmtCZ0hLZGktbDZDRH5IcDVrSVFBQUFBJCQAAAAAAAAAAAEAAACoCI6a0bDDzlhMWAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP-RdmT~kXZkOF; BDUSS_BFESS=BsMWdXOEc4czVESXI1fkFsaEZXZVBYdUlFQjBwUmtCZ0hLZGktbDZDRH5IcDVrSVFBQUFBJCQAAAAAAAAAAAEAAACoCI6a0bDDzlhMWAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP-RdmT~kXZkOF; MCITY=-163%3A; BAIDUID_BFESS=EB6DC9353F2DD763B13070C217387213:FG=1; BDRCVFR[oxw6P5LCmIs]=mk3SLVN4HKm; delPer=0; PSINO=7; BA_HECTOR=85a5a580058085a1a580a0291ieei3l1o; BDORZ=FFFB88E999055A3F8A630C64834BD6D0; ZFY=qArVloTxerdIjb2yWZ:BNZoAEM8uprq:AL0glgX0P:AuMQ:C; BCLID=8493038945101704499; BCLID_BFESS=8493038945101704499; BDSFRCVID=CE4OJexroG0ZmSbfTPQNKlZRGLweG7bTDYrEOwXPsp3LGJLVFakFEG0Pts1-dEu-S2OOogKKQgOTHRuF_2uxOjjg8UtVJeC6EG0Ptf8g0M5; BDSFRCVID_BFESS=CE4OJexroG0ZmSbfTPQNKlZRGLweG7bTDYrEOwXPsp3LGJLVFakFEG0Pts1-dEu-S2OOogKKQgOTHRuF_2uxOjjg8UtVJeC6EG0Ptf8g0M5; H_BDCLCKID_SF=tRAOoC_-tDvDqTrP-trf5DCShUFs3MLJB2Q-XPoO3KJADfOPblOm0fCUhb6N0M7f5mkf3fbgy4op8P3y0bb2DUA1y4vp0toW3eTxoUJ2-KDVeh5Gqq-KXU4ebPRiQ4b9QgbNLlQ7tt5W8ncFbT7l5hKpbt-q0x-jLTnhVn0MBCK0HPonHjLKD5J33f; H_BDCLCKID_SF_BFESS=tRAOoC_-tDvDqTrP-trf5DCShUFs3MLJB2Q-XPoO3KJADfOPblOm0fCUhb6N0M7f5mkf3fbgy4op8P3y0bb2DUA1y4vp0toW3eTxoUJ2-KDVeh5Gqq-KXU4ebPRiQ4b9QgbNLlQ7tt5W8ncFbT7l5hKpbt-q0x-jLTnhVn0MBCK0HPonHjLKD5J33f; REALTIME_TRANS_SWITCH=1; HISTORY_SWITCH=1; FANYI_WORD_SWITCH=1; SOUND_SPD_SWITCH=1; SOUND_PREFER_SWITCH=1; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1692878969; BDRCVFR[A7hGrXFW48R]=K7PavkeTFPTUAN8ULuEQhPEUi4WU6; H_PS_PSSID=; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1692888596; ab_sr=1.0.1_YzBmNTg5ZDVhM2M1ZmI0ZDg2ZWVhMmE3NGEyZWFjMWJiOTVhNTZhNmNkOWY2ODIxMjlkNWZiNjM3NjI0YzkwZjY4YzlmNjZjYTc2ZGY3NTJlYmRhMTU5NjUzY2RkNDUzNDMxMTA5OTU3OGJkYjk1N2E3MzE1MGE0YmRkOTdhN2I2NmJkOGZhMjllNjQ3ZDkzZmZiNzM5ZDQ2NzBkZTk4ZmQ2NmYxY2Q2MzlhMzlkOTgwM2RjNWNmZjcwNDE2ZTUz"
}
# 定制请求对象
requestObject=urllib.request.Request(url=base_url+"?"+urllib.parse.urlencode(param),data=urllib.parse.urlencode(data).encode('utf8'),headers=headers,method="POST")
response=urllib.request.urlopen(requestObject)
content= response.read().decode('utf8')
res= json.loads(content)
print("trans_result:",res["trans_result"]["data"][0]["dst"])